<?php

/**
 * 插件中心
 */

namespace app\ph\controller;

use think\facade\Request;

class Tool extends Base
{
    // sitemap 存储路径
    protected $savePath;

    public function __construct()
    {
        parent::__construct();
        $this->savePath = './sitemap/';
    }

    /**
     * 插件中心
     * @return \think\response\View
     */
    public function index()
    {
        return view('tool/index');
    }

    /**
     * sitemap文件地图功能
     * @param string $path
     * @return \think\response\View
     */
    public function map($path = '')
    {
        if ($path) {
            //删除文件
            if (!file_exists($this->savePath . $path)) {
                $this->error('文件不存在');
            } else {
                unlink($this->savePath . $path);
                $this->success('删除成功');
            }
        }
        if (!file_exists($this->savePath)) {
            mkdir($path, 0755);
        }
        // 获取目录下文件
        $file = scandir($this->savePath);
        unset($file[0], $file[1]);
        $data  = [];
        $start = 1;
        foreach ($file as $k => $v) {
            $data[$k]['num']  = $start;
            $data[$k]['name'] = $v;
            $filePath         = './sitemap/' . $v;
            if (file_exists($filePath)) {
                $data[$k]['size'] = $this->getSize(filesize($filePath));
            } else {
                $data[$k]['size'] = '未找到文件';
            }
            $start += 1;
        }
        return view('tool/map',
            [
                'data' => $data
            ]
        );
    }

    /**
     * 文件大小格式
     * @param $fileSize
     * @return string
     */
    private function getSize($fileSize)
    {
        if ($fileSize >= 1073741824) {
            $fileSize = round($fileSize / 1073741824 * 100) / 100 . ' GB';
        } elseif ($fileSize >= 1048576) {
            $fileSize = round($fileSize / 1048576 * 100) / 100 . ' MB';
        } elseif ($fileSize >= 1024) {
            $fileSize = round($fileSize / 1024 * 100) / 100 . ' KB';
        } else {
            $fileSize = $fileSize . ' 字节';
        }
        return $fileSize;
    }

    /**
     * 百度站长普通收录配置
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function push()
    {
        if (Request::isPost()) {
            $data['site']  = input('post.site');
            $data['token'] = input('post.token');
            $res           = \app\common\model\System::where(['id' => 1])->update($data);
            return operationResult($res);
        } else {
            $data = \app\common\model\System::where(['id' => 1])->field('site,token')->find();
            return view('tool/push',
                [
                    'data' => $data
                ]
            );
        }
    }

    /**
     * robots 设置
     * @return \think\response\View
     */
    public function robots()
    {
        $data = file_get_contents('./robots.txt');
        return view('tool/robots',
            [
                'data' => $data
            ]
        );
    }

    /**
     * robots保存
     * @return \think\response\Json
     */
    public function robotsSave()
    {
        $data = input('post.data');
        $res  = file_put_contents('./robots.txt', $data);
        return operationResult($res);
    }
}
